﻿namespace Nomo.UnityEditorModule
{
    [UnityEditor.CanEditMultipleObjects]
    [UnityEditor.CustomEditor(typeof(UnityCoreModule.UI.Ring))]
    internal sealed class RingEditor : UnityEditor.UI.GraphicEditor
    {
        private UnityCoreModule.UI.Ring        _ring;
        private UnityEditor.SerializedProperty _texture;
        private UnityEditor.SerializedProperty _segments;
        private UnityEditor.SerializedProperty _normalized;
        private UnityEditor.SerializedProperty _innerCircleDiameter;
        private UnityEngine.GUIContent         _segmentsGUIContent;
        private UnityEngine.GUIContent         _normalizedGUIContent;
        private UnityEngine.GUIContent         _innerCircleDiameterGUIContent;

        /// <inheritdoc />
        protected override void OnEnable()
        {
            base.OnEnable();
            _ring                          = serializedObject.targetObject as UnityCoreModule.UI.Ring;
            _texture                       = serializedObject.FindProperty("texture");
            _segments                      = serializedObject.FindProperty("segments");
            _normalized                    = serializedObject.FindProperty("normalized");
            _innerCircleDiameter           = serializedObject.FindProperty("innerCircleDiameter");
            _segmentsGUIContent            = new UnityEngine.GUIContent(nameof(UnityCoreModule.UI.Ring.Segments),            "边数");
            _normalizedGUIContent          = new UnityEngine.GUIContent(nameof(UnityCoreModule.UI.Ring.Normalized),          "是否使用标准化单位长度来表示内圆的直径？");
            _innerCircleDiameterGUIContent = new UnityEngine.GUIContent(nameof(UnityCoreModule.UI.Ring.InnerCircleDiameter), "内圆的直径");
            SetShowNativeSize(_texture.objectReferenceValue != null, true);
        }

        /// <inheritdoc />
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            UnityEditor.EditorGUILayout.PropertyField(_texture);
            AppearanceControlsGUI();
            UnityEditor.EditorGUILayout.PropertyField(_segments, _segmentsGUIContent);
            UnityEditor.EditorGUI.BeginChangeCheck();
            UnityEditor.EditorGUILayout.PropertyField(_normalized, _normalizedGUIContent);
            if (UnityEditor.EditorGUI.EndChangeCheck())
            {
                var innerCircleDiameter = _innerCircleDiameter.floatValue;
                var r                   = _ring.GetPixelAdjustedRect();
                var minSide             = UnityEngine.Mathf.Min(r.width, r.height);
                switch (_normalized.boolValue)
                {
                    case true:
                        _innerCircleDiameter.floatValue = minSide != 0F ? UnityEngine.Mathf.Clamp01(innerCircleDiameter / minSide) : 0F;
                        break;
                    default:
                        _innerCircleDiameter.floatValue = innerCircleDiameter * minSide;
                        break;
                }
            }
            switch (_normalized.boolValue)
            {
                case true:
                    UnityEditor.EditorGUILayout.Slider(_innerCircleDiameter, 0F, 1F, _innerCircleDiameterGUIContent);
                    break;
                default:
                    var r = _ring.GetPixelAdjustedRect();
                    UnityEditor.EditorGUILayout.Slider(_innerCircleDiameter, 0F, UnityEngine.Mathf.Min(r.width, r.height), _innerCircleDiameterGUIContent);
                    break;
            }
            SetShowNativeSize(_texture.objectReferenceValue != null, false);
            NativeSizeButtonGUI();
            serializedObject.ApplyModifiedProperties();
        }
    }
}
